<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
description("Tests the RTCPeerConnection constructor.");

shouldBe("webkitRTCPeerConnection", "RTCPeerConnection");

shouldNotThrow("new RTCPeerConnection(null);");
shouldNotThrow("new RTCPeerConnection(undefined);");
shouldNotThrow("new RTCPeerConnection({});");
shouldNotThrow("new RTCPeerConnection();");
shouldThrow("new RTCPeerConnection('');");

shouldNotThrow("new RTCPeerConnection({iceServers:[]});");
shouldNotThrow("new RTCPeerConnection({iceServers:[{url:'stun:foo.com'}]});");
shouldNotThrow("new RTCPeerConnection({iceServers:[{url:'turn:foo.com', username:'x', credential:'x'}]});");
shouldNotThrow("new RTCPeerConnection({iceServers:[{url:'turn:foo.com', username:'x', credential:'x'},{url:'stun:bar.com'}]});");
shouldNotThrow("new RTCPeerConnection({iceServers:[{urls:'stun:foo.com'}]});");
shouldNotThrow("new RTCPeerConnection({iceServers:[{urls:['stun:foo.com', 'turn:foo.com'], username:'x', credential:'x'}]});");
shouldThrow("new RTCPeerConnection({iceServers:[{urls:['stun:foo.com', 'turn:foo.com']}]});");

shouldNotThrow("new RTCPeerConnection({fooServers:[]});");
shouldThrow("new RTCPeerConnection({iceServers:true});");
shouldThrow("new RTCPeerConnection({iceServers:[1, 2, 3]});");
shouldThrow("new RTCPeerConnection({iceServers:[{}]});");
shouldThrow("new RTCPeerConnection({iceServers:[{url:'foo'}]});");
shouldThrow("new RTCPeerConnection({iceServers:[{urls:'unsupported:scheme'}]});");
shouldThrow("new RTCPeerConnection({iceServers:[{urls:[1, 'turn:foo.com']}]});");

shouldNotThrow("new RTCPeerConnection({iceServers:[], iceTransportPolicy:'relay'});");
shouldNotThrow("new RTCPeerConnection({iceServers:[], iceTransportPolicy:'all'});");
shouldThrow("new RTCPeerConnection({iceServers:[], iceTransportPolicy:'none'});");
shouldThrow("new RTCPeerConnection({iceServers:[], iceTransportPolicy:'foo'});");

// TODO(foolip): |iceTransports| is not in the spec.
shouldNotThrow("new RTCPeerConnection({iceServers:[], iceTransports:'relay'});");
shouldNotThrow("new RTCPeerConnection({iceServers:[], iceTransports:'all'});");
shouldThrow("new RTCPeerConnection({iceServers:[], iceTransports:'none'});");
shouldThrow("new RTCPeerConnection({iceServers:[], iceTransports:'foo'});");

shouldNotThrow("new RTCPeerConnection({iceServers:[], bundlePolicy:'balanced'});");
shouldNotThrow("new RTCPeerConnection({iceServers:[], bundlePolicy:'max-bundle'});");
shouldNotThrow("new RTCPeerConnection({iceServers:[], bundlePolicy:'max-compat'});");
shouldThrow("new RTCPeerConnection({iceServers:[], bundlePolicy:'foo'});");

shouldNotThrow("new RTCPeerConnection({iceServers:[], rtcpMuxPolicy:'negotiate'});");
shouldNotThrow("new RTCPeerConnection({iceServers:[], rtcpMuxPolicy:'require'});");
shouldThrow("new RTCPeerConnection({iceServers:[], rtcpMuxPolicy:'foo'});");

shouldNotThrow("new RTCPeerConnection({iceCandidatePoolSize:0});");
shouldNotThrow("new RTCPeerConnection({iceCandidatePoolSize:1});");
shouldNotThrow("new RTCPeerConnection({iceCandidatePoolSize:255});");
shouldThrow("new RTCPeerConnection({iceCandidatePoolSize:-1});");
shouldThrow("new RTCPeerConnection({iceCandidatePoolSize:99999999});");
shouldThrow("new RTCPeerConnection({iceCandidatePoolSize:256});");
shouldThrow("new RTCPeerConnection({iceCandidatePoolSize:'foo'});");

// Deprecated.
shouldNotThrow("new RTCPeerConnection(null, {mandatory:{valid_and_supported_1:1}});");
shouldNotThrow("new RTCPeerConnection(null, {mandatory:{valid_and_supported_1:1, valid_and_supported_2:1}});");
shouldNotThrow("new RTCPeerConnection(null, {optional:[{valid_and_supported_1:0}]});");
shouldNotThrow("new RTCPeerConnection(null, {optional:[{valid_and_supported_1:0},{valid_and_supported_2:0}]});");
shouldNotThrow("new RTCPeerConnection(null, {optional:[{valid_but_unsupported_1:0},{valid_but_unsupported_2:0}]});");
shouldThrow("new RTCPeerConnection(null, {mandatory:{valid_and_supported_1:66}});");
shouldThrow("new RTCPeerConnection(null, {mandatory:{invalid:1}});");
shouldThrow("new RTCPeerConnection(null, {mandatory:{valid_but_unsupported_1:1}});");
shouldThrow("new RTCPeerConnection(null, {mandatory:{valid_but_unsupported_1:1, valid_and_supported_1:1}});");
shouldThrow("new RTCPeerConnection(null, {optional:{valid_and_supported_1:0}});");
shouldThrow("new RTCPeerConnection(null, {optional:[{valid_and_supported_1:0,valid_and_supported_2:0}]});");
// Optional constraints are ignored even if they are invalid.
shouldNotThrow("new RTCPeerConnection(null, {optional:[{invalid:0}]});");
shouldThrow("new RTCPeerConnection(null, {valid_and_supported_1:1});");
shouldThrow("new RTCPeerConnection(null, {valid_but_unsupported_1:1});");
shouldThrow("new RTCPeerConnection(null, {valid_and_supported_2:1, mandatory:{valid_and_supported_1:1}});");

// Construct with certificates.
shouldThrow("new RTCPeerConnection({iceServers:[], certificates:null});");
shouldNotThrow("new RTCPeerConnection({iceServers:[], certificates:undefined});");
shouldNotThrow("new RTCPeerConnection({iceServers:[], certificates:[]});");
shouldThrow("new RTCPeerConnection({iceServers:[], certificates:[null]});");
shouldThrow("new RTCPeerConnection({iceServers:[], certificates:[1337]});");
// Global certificate variables so that the "should..." methods can evaluate them.
var certRSA = null;
var certECDSA = null;
var certExpired = null;

function testCertificates1RSA()
{
    RTCPeerConnection.generateCertificate({ name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" })
        .then(function(certificate) {
                  certRSA = certificate;
                  shouldNotThrow('new RTCPeerConnection({iceServers:[], certificates:[certRSA]}, null);');
                  testCertificates2ECDSA();
              },
              function() {
                  testFailed('Generating RSA 2048');
                  testCertificates2ECDSA();
              });
}
function testCertificates2ECDSA()
{
    RTCPeerConnection.generateCertificate({ name: "ECDSA", namedCurve: "P-256" })
        .then(function(certificate) {
                  certECDSA = certificate;
                  shouldNotThrow('new RTCPeerConnection({iceServers:[], certificates:[certECDSA]}, null);');
                  testCertificates3Expired();
              },
              function() {
                  testFailed('Generating ECDSA P-256');
                  testCertificates3Expired();
              });
}
function testCertificates3Expired()
{
    RTCPeerConnection.generateCertificate({ name: "ECDSA", namedCurve: "P-256", expires:0 })
        .then(function(certificate) {
                  certExpired = certificate;
                  shouldBeTrue('certExpired.expires <= new Date().getTime()');
                  shouldThrow('new RTCPeerConnection({iceServers:[], certificates:[certExpired]}, null);');
                  finishJSTest();
              },
              function() {
                  testFailed('Generating ECDSA P-256');
                  finishJSTest();
              });
}
// Sequentially test construction with RSA and ECDSA certificates.
// testCertificates3Expired's callback methods mark the end of the async tests.
testCertificates1RSA();

window.jsTestIsAsync = true;
window.successfullyParsed = true;
</script>
</body>
</html>
